package gwtmodule1.client.gui.salesdept;

import gwtmodule1.client.gui.utils.DekoTable;
import gwtmodule1.client.gui.utils.FrameClient;
import gwtmodule1.client.gui.utils.HSpacer;
import gwtmodule1.client.gui.utils.Title;

import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class PageSalesReport extends VerticalPanel implements FrameClient {

	private class SalesReportTable extends DekoTable {

		private float totalTax = 0;

		private float totalPrice = 0;

		public SalesReportTable() {
			super();
			addTitle("Brand");
			addTitle("Product");
			addTitle("Quantity");
			addTitle("Price");
			addTitle("Discount");
			addTitle("Tax");
			addTitle("Total");
		}

		public void addItem(String brand, String product, SellUnit[] units,
				float tax) {
			float total = 0;
			for (int i = 0; i < units.length; i++)
				total += units[i].calcTotal();
			float taxval = total * (tax / 100);
			total += taxval;
			totalPrice += total;
			totalTax += taxval;

			AddRow r = addRow();
			r.add(brand).setRowspan(units.length);
			r.add(product).setRowspan(units.length);
			addSellUnit(r, units[0]);
			r.add(taxval, "USD").setRowspan(units.length);
			r.add(total, "USD").setRowspan(units.length);

			for (int i = 1; i < units.length; i++)
				addSellUnit(addRow(), units[i]);
		}

		private void addSellUnit(AddRow r, SellUnit u) {
			r.add(u.quantity);
			r.add(u.price, "USD");
			r.add(u.discount, "%");
		}

		public void addTotal() {
			AddRow r = addRow();
			r.add("").setColspan(5);
			r.add(totalTax, "USD").bold();
			r.add(totalPrice, "USD").bold();
		}

	}

	private static class SellUnit {
		private int quantity;

		private float price;

		private int discount;

		public SellUnit(int quantity, float price, int discount) {
			super();
			this.quantity = quantity;
			this.price = price;
			this.discount = discount;
		}

		public float calcTotal() {
			return quantity * price * (1f - (discount / 100f));
		}
	}

	public PageSalesReport() {
		super();
		add(new Title("Sales Report"));
		add(createDateSel());
		add(new HSpacer());
		add(createReportTable());
	}

	private Widget createDateSel() {
		HorizontalPanel panel = new HorizontalPanel();
		panel.add(new HTML("Select Day:"));
		panel.add(new TextBox());
		return panel;
	}

	private Widget createReportTable() {
		SalesReportTable table = new SalesReportTable();
		SellUnit[] xbox = { new SellUnit(4, 30.50f, 5) };
		SellUnit[] ps = { new SellUnit(10, 55.99f, 10),
				new SellUnit(55, 69.99f, 10) };
		SellUnit[] wii = { new SellUnit(3, 44, 0), new SellUnit(32, 44, 5),
				new SellUnit(77, 44, 10) };
		SellUnit[] ip = { new SellUnit(100, 49.99f, 0) };
		SellUnit[] gk = { new SellUnit(10, 50f, 0), new SellUnit(2, 100f, 0) };
		table.addItem("Microsoft", "XBox", xbox, 5.5f);
		table.addItem("Sony", "Playstation", ps, 5.5f);
		table.addItem("Nintendo", "Wii", wii, 5.5f);
		table.addItem("Apple", "iPhone", ip, 5.5f);
		table.addItem("QAY", "Gift Card", gk, 0);
		table.addTotal();
		return table;
	}

	public void onShow(Object param) {

	}

}
